package de.riceman.simpleGo.view.views.visualizationView;

public class SpringEdge extends Edge {

	double stiffness;
	double naturalLength;
  
  //This edge subclass applies a spring force between the two nodes it connects
  //The spring force formula is F = stiffness(currentLength-naturalLength)
  //This equation is one-dimensional, and applies to the straight line
  //between the two nodes.
  
	public SpringEdge(ForcedNode a, ForcedNode b, double stiffness, double naturalLength) {
		super(a, b);
		this.stiffness = stiffness;
		this.naturalLength = naturalLength;
	}

	public void setNaturalLength(double naturalLength) {
		this.naturalLength = naturalLength;
	}
  
	public double getNaturalLength() {
		return naturalLength;
  	}
  
  	public ForcedNode getVertexA() {
		
  		return (ForcedNode)vertexA;
  	}
	
	public void setVertexA(ForcedNode vertexA) {
	
		this.vertexA = vertexA;
	}
	
	public ForcedNode getVertexB() {
	
		return (ForcedNode)vertexB;
	}
	
	public void setVertexB(ForcedNode vertexB) {
	
		this.vertexB = vertexB;
	}
	
	public double dX() {
		return vertexA.getPosition().getX() - vertexB.getPosition().getX();
	}
	  
	public double dY() {
		return vertexA.getPosition().getY() - vertexB.getPosition().getY();
	}
	
  public Vector3D getForceA() {
	  double dx = this.dX();
	  double dy = this.dY();
	  double l = Math.sqrt(dx*dx + dy*dy);
	  double f = stiffness*(l-naturalLength);
    
    return new Vector3D(-f*dx/l, -f*dy/l, 0);
  }
    
  public Vector3D getForceB() {
	  double dx = this.dX();
	  double dy = this.dY();
	  double l = Math.sqrt(dx*dx + dy*dy);
	  double f = stiffness*(l-naturalLength);
    
    return new Vector3D(f*dx/l, f*dy/l, 0);
  }
}
